package top.tangyh.lamp.care.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.tangyh.basic.exception.BizException;
import top.tangyh.lamp.care.entity.BEquipmentBrand;
import top.tangyh.lamp.care.entity.BPgSrx;
import top.tangyh.lamp.care.mapper.BDevDetailMapper;
import top.tangyh.lamp.care.mapper.BEquipmentBrandMapper;
import top.tangyh.lamp.care.service.BDevDetailService;
import top.tangyh.basic.base.service.impl.SuperServiceImpl;
import top.tangyh.lamp.care.manager.BDevDetailManager;
import top.tangyh.lamp.care.entity.BDevDetail;
import top.tangyh.lamp.care.vo.query.DevDetailSearchVO;
import top.tangyh.lamp.care.vo.result.DevDetailDto;
import top.tangyh.lamp.care.vo.result.PageOutputDTO;
import top.tangyh.lamp.care.vo.save.BDevDetailSaveVO;
import top.tangyh.lamp.care.vo.update.BDevDetailUpdateVO;
import top.tangyh.lamp.care.vo.result.BDevDetailResultVO;
import top.tangyh.lamp.care.vo.query.BDevDetailPageQuery;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 监护仪设备详情表
 * </p>
 *
 * @author zqb
 * @date 2024-07-15 16:42:41
 * @create [2024-07-15 16:42:41] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class BDevDetailServiceImpl extends SuperServiceImpl<BDevDetailManager, Long, BDevDetail> implements BDevDetailService {

    private final BDevDetailMapper bDevDetailMapper;
    private final BEquipmentBrandMapper bEquipmentBrandMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveDevDetail(BDevDetail po) {
        int countCode=this.selectCountOfDevDetailByDevCode(po.getDevCode(),null);
        if(countCode>0){
            throw new BizException("设备识别号已存在，请勿重复添加！");
        }
        int countIp=this.selectCountOfDevDetailByIP(po.getIp(),null);
        if(countIp>0){
            throw new BizException("ip已存在，请勿重复添加！");
        }
        int result=bDevDetailMapper.insert(po);
        if(result>0){
            return true;
        }else{
            return false;
        }

    }

    @Override
    public int selectCountOfDevDetailByDevCode(String devCode,Long id) {
        QueryWrapper<BDevDetail> queryWrapper=new QueryWrapper();
        queryWrapper.ne(null!=id,"id",id);
        queryWrapper.eq("dev_code",devCode);
        queryWrapper.eq("is_deleted",0);

        Integer count = bDevDetailMapper.selectCount(queryWrapper).intValue();;
        return count;
    }

    @Override
    public int selectCountOfDevDetailByIP(String ip,Long id) {
        QueryWrapper<BDevDetail> queryWrapper=new QueryWrapper();
        queryWrapper.ne(null!=id,"id",id);
        queryWrapper.eq("ip",ip);
        queryWrapper.eq("is_deleted",0);

        Integer count = bDevDetailMapper.selectCount(queryWrapper).intValue();
        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateDevDetail(BDevDetail po) {
        int countCode=this.selectCountOfDevDetailByDevCode(po.getDevCode(),po.getId());
        if(countCode>0){
            throw new BizException("设备识别号已存在，请勿重复添加");
        }
        int countIp=this.selectCountOfDevDetailByIP(po.getIp(),po.getId());
        if(countIp>0){
            throw new BizException("ip已存在，请勿重复添加！");
        }
        int result=bDevDetailMapper.updateById(po);
        if(result>0){
            return true;
        }else{
            return false;
        }

    }

    @Override
    public List<String> getdevCodeList(String devCode) {
        QueryWrapper<BDevDetail> wrapper = new QueryWrapper<>();
        wrapper.like("dev_code", devCode);
        List<BDevDetail> list = bDevDetailMapper.selectList(wrapper);
        List<String> listDevCode = list.stream().map(BDevDetail::getDevCode).collect(Collectors.toList());
        return listDevCode;
    }


    @Override
    public PageOutputDTO<DevDetailDto> queryPage(DevDetailSearchVO query) {
        PageHelper.startPage(query.getPage(),query.getSize());
        PageInfo pageInfo = new PageInfo<>(bDevDetailMapper.selectPageOfDevicesByParam(query));

        return new PageOutputDTO<>(pageInfo.getTotal(),
                (long)pageInfo.getPages(),
                (long)pageInfo.getPageNum(),
                BeanUtil.copyToList(pageInfo.getList(), DevDetailDto.class)
                );


        //ConvertUtil.convertList(pageInfo.getList(), DevDetailDto.class)
    }

    @Override
    public int selectCountOfBindDev(String devCode) {
        return bDevDetailMapper.selectCountOfBindDev(devCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateList(List<BDevDetail> list) {
        bDevDetailMapper.updateById(list);
        return true;
    }

    @Override
    public BDevDetailResultVO selectOneQuery(String devCode) {
        QueryWrapper<BDevDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("dev_code", devCode);
        wrapper.eq("is_deleted", 0);


        BDevDetail devDetail = bDevDetailMapper.selectOne(wrapper);

//        BEquipmentBrand bEquipmentBrand=bEquipmentBrandMapper.selectById(devDetail.getBrand());
        BEquipmentBrand bEquipmentBrand = Optional.ofNullable(devDetail)
                .map(BDevDetail::getBrand)
                .map(bEquipmentBrandMapper::selectById)
                .orElse(null);
        BDevDetailResultVO devDetailResultVO=BeanUtil.copyProperties(devDetail,BDevDetailResultVO.class);
        if(null!=bEquipmentBrand){
            devDetailResultVO.setBrandName(bEquipmentBrand.getBrandName());
        }

        return devDetailResultVO;
    }

    @Override
    public int getCountByDevCode(String devCode) {
        return bDevDetailMapper.getCountByDevCode(devCode);
    }
}


